﻿using LxBasic;
using LxLESS;

namespace LxCore
{
    public class FvJoint : JsIdentifyItem, IBuildMGJoint, IIndexHost
    {
        public FvJoint(FvMesh inMesh)
        {
            Name = "新接触";
            InMesh = inMesh;
        }
        public FvJoint(FvMesh inMesh, FvZone oZone, FvBound oBound, FvZone nZone, FvBound nBound) : this(inMesh)
        {
            this.OZone = oZone; this.OBound = oBound; this.NZone = nZone; this.NBound = nBound;
        }
        //public bool IsConode { get; set; } = true;

        public FvJoiFace[] JoiFaces;
        public LESSJoint LESSJoint { get; set; }
        public IndexRange CellIdx => new IndexRange(JoiFaces.Length);
        public IndexRange FaceIdx => new IndexRange(JoiFaces.Length);
        //not local
        public FvMesh InMesh { get; set; }
        public FvZone OZone;
        private FvBound _OBound;
        public FvBound OBound { get => _OBound; set { _OBound = value; _OBound.InJoint = this; } }
        public FvZone NZone;
        private FvBound _NBound;
        public FvBound NBound { get => _NBound; set { _NBound = value; _NBound.InJoint = this; } }
        public void CheckCreate()
        {
            if (LESSJoint != null) return;
            var O_BouFaces = OZone.BouFaces;
            var N_BouFaces = NZone.BouFaces;
            //if (IsConode)
            //{
            CheckCreator.ArrayStrict(ref JoiFaces, OBound.FaceIdx.GetCount());
            int ofStart = OBound.FaceIdx.Start;
            int nfStart = NBound.FaceIdx.Start;
            for (int i1 = 0; i1 < JoiFaces.Length; i1++)
            {
                ref var jf = ref JoiFaces[i1];
                jf.O_BouFace = i1 + ofStart;
                jf.N_BouFace = i1 + nfStart;
                ref var obf = ref O_BouFaces[jf.O_BouFace];
                ref var nbf = ref N_BouFaces[jf.N_BouFace];
                jf.O_InnCell = obf.OSideCell;
                jf.N_InnCell = nbf.OSideCell;
            }
            //}
            //else
            //{
            //    LxLog.Error("暂不支持非共节点界面");
            //}
            {
                LESSJoint = new LESSJoint();
                LESSJoint.CheckCreate(this, OZone.LESSZone, OBound.ID,
                    NZone.LESSZone, NBound.ID);
            }
        }

        IEnumerable<int> IBuildMGJoint.GetOZoneSideJoints(int sideId)
        {
            yield return sideId - OBound.FaceIdx.Start;
        }

        IEnumerable<int> IBuildMGJoint.GetNZoneSideJoints(int sideId)
        {
            yield return sideId - NBound.FaceIdx.Start;
        }

        IndexRange IBuildMGJoint.GetFaceIdx()
        {
            return FaceIdx;
        }
        public override void FromJsonKV(string key, IJsonReader jsReader)
        {
            switch (key)
            {
                case nameof(OZone): OZone = InMesh.Zones[jsReader.ReadString()]; break;
                case nameof(NZone): NZone = InMesh.Zones[jsReader.ReadString()]; break;
                case nameof(OBound): OBound = OZone.Bounds[jsReader.ReadString()]; break;
                case nameof(NBound): NBound = NZone.Bounds[jsReader.ReadString()]; break;
                default: base.FromJsonKV(key, jsReader); break;
            }
        }
        public override void ToJsonKV(IJsonWriter jsWriter)
        {
            base.ToJsonKV(jsWriter);
            jsWriter.WriteString(nameof(OZone), OZone.Name);
            jsWriter.WriteString(nameof(OBound), OBound.Name);
            jsWriter.WriteString(nameof(NZone), NZone.Name);
            jsWriter.WriteString(nameof(NBound), NBound.Name);
        }
    }
}